<?php

namespace app\common\service\swoole;

use think\App;
use app\BaseController;

class Chat extends BaseController
{
    private $server;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->server = new Swoole\Server('127.0.0.1', 9501);
        $this->server->on('Connect', [$this, 'onConnect']);
        $this->server->on('Receive', [$this, 'onReceive']);
        $this->server->on('Close', [$this, 'onClose']);
    }

    public function onConnect($server, $fd)
    {
        echo "客户端ID:{$fd}链接.\n";
    }

    public function onReceive($server, $fd, $fromId, $data)
    {
        $server->send($fd, '发送数据：' . $data);
    }

    public function onClose($server, $fd)
    {
        echo "客户端ID:{$fd}关闭.\n";
    }

}
